package com.teamin.web.common;

import com.teamin.config.ErrorCode;
import com.teamin.web.common.exception.DataException;
import com.teamin.web.common.exception.FileNotExistException;
import com.teamin.web.common.exception.NotBelongCircleException;
import com.teamin.web.common.exception.UnauthorizedException;
import com.teamin.web.model.ErrorInfo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @Author: yangrui
 * @Description: 自定义异常处理类
 * @Date: 下午2:33 2017/11/21
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    /**
     * @Author: yangrui
     * @Description: 数据异常处理
     * @Date: 下午5:29 2017/10/13
     */
    @ExceptionHandler(DataException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Object handleDataException() {
        return new ErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.DATA_FAIL);
    }

    /**
     * @Author: yangrui
     * @Description: 文件不存在自定义异常处理
     * @Date: 下午5:29 2017/10/13
     */
    @ExceptionHandler(FileNotExistException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Object handleFileNotExistException() {
        return new ErrorInfo(HttpStatus.NO_CONTENT, ErrorCode.NOT_FOUND);
    }

    /**
     * @Author: yangrui
     * @Description: 不属于圈子自定义异常处理
     * @Date: 下午3:11 2017/11/24
     */
    @ExceptionHandler(NotBelongCircleException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Object handleNotBelongCircleException() {
        return new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FORBIDDEN_VIEW_CIRCLE);
    }

    /**
     * @Author: yangrui
     * @Description: 未授权自定义异常处理
     * @Date: 下午7:01 2017/11/29
     */
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Object handleUnauthorizedException() {
        return new ErrorInfo(HttpStatus.UNAUTHORIZED, ErrorCode.UNAUTHORIZED);
    }
}
